package com.hc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.common.bean.PageBean;
import com.hc.common.enums.ResultEnum;
import com.hc.constant.GlobalConstant;
import com.hc.domain.Product;
import com.hc.ex.GlobalException;
import com.hc.mapper.ProductMapper;
import com.hc.query.ProductCondition;
import com.hc.res.ProductItem;
import com.hc.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
/**
 * @author hecai
 * @date 2021/12/7 15:10
 *
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService{

    @Resource
    private ProductMapper productMapper;
    @Override
    public int updateBatch(List<Product> list) {
        return baseMapper.updateBatch(list);
    }
    @Override
    public int batchInsert(List<Product> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public PageBean<ProductItem> queryByCondition(ProductCondition condition) {
        // 条件为空 抛出全局异常 ，提示参数错误
        if (condition == null){
            throw new GlobalException(ResultEnum.PARAM_ERROR);
        }
        // 获取分页的页码和大小
        final Long current = condition.getCurrent() == null ? 1 : condition.getCurrent();
        final Integer pageSize = condition.getPageSize() == null ? GlobalConstant.PageSize.DEFALUT_PAGE_SIZE : condition.getPageSize();
        IPage<ProductItem> page = new Page<>(current, pageSize);
        // 调用mapper 层的方法查询数据库数据并封装在page中
        productMapper.selectList(page,condition);
        // page转化为pagePean输出
        PageBean<ProductItem> productItemPageBean = PageBean.init(page);
        return productItemPageBean;
    }
}